*****************************************************************************
* 																			*
*			Campaign Panel Book Chapter 4 - Intraindividual Dynamic Turnout								*		
*																			*
*****************************************************************************

* The dynamics analyses proceed according to the same scheme as shown in do-file "dynamic reception" and
* will therefore not be expained again.


*---------------------------------------------------------------
* Creation of main diagonal for successive wave pairs and combined for all waves
*---------------------------------------------------------------



foreach num1 of numlist 1/7  {
foreach num2 of numlist 1/7  {
	if `num1' != `num2' & `num1'<`num2'{ 
		foreach var1 of varlist a_beteil`num1'  {		
		gen a_beteil`num1'`num2'_hd=.
		replace a_beteil`num1'`num2'_hd=1 if a_beteil`num1'==a_beteil`num2' & a_beteil`num1'<. & a_beteil`num2'<.
		replace a_beteil`num1'`num2'_hd=0 if a_beteil`num1'!=a_beteil`num2' & a_beteil`num1'<. & a_beteil`num2'<.
		lab var a_beteil`num1'`num2'_hd "Hauptdiagonale: beteil 2009"
		tab a_beteil`num1'`num2'_hd [aweight=a_weight], m				
				}
		foreach var1 of varlist b_beteil`num1'  {		
		gen b_beteil`num1'`num2'_hd=.
		replace b_beteil`num1'`num2'_hd=1 if b_beteil`num1'==b_beteil`num2' & b_beteil`num1'<. & b_beteil`num2'<.
		replace b_beteil`num1'`num2'_hd=0 if b_beteil`num1'!=b_beteil`num2' & b_beteil`num1'<. & b_beteil`num2'<.
		lab var b_beteil`num1'`num2'_hd "Hauptdiagonale: beteil 2009"
		tab b_beteil`num1'`num2'_hd [aweight=b_weight], m				
				}
		}
	}
	}


****Aggregation on a complete main diagonal for all successive waves

foreach year in a b {
	foreach var in `year'_beteil { 
		gen `var'_hd_ges=(`var'12_hd+`var'23_hd+`var'34_hd+`var'45_hd+`var'56_hd+`var'67_hd)/6
	}
}

****Drop single main diagonals


foreach year in a b {
	foreach var in `year'_beteil {
		drop `var'??_hd
	}
}


*We save the data before running the intra-individual regressions
save "${data}\data2009_2013 participants 7 waves.dta", replace


 
*---------------------------------------------------------------------
* Intraindividual Dynamic: Indivdiual Regressions over Time
*----------------------------------------------------------------------

* Create ID-variables for loops

/*Later the loops are supposed to calculate linear regressions of reception mode
over time for each individual respondent looping over all respondents. Since not
all respondents possess valid measures on every reception variable (non-response/don't know)
the loop would stop working if it encountered such a case. The ID-variables are
created for those respondents only who do not have missing values in order to 
guarantee the functioning of the loop.*/



foreach year in a b {
foreach var in beteil {
egen `year'_id_`var' = group(lfdn) if `year'_`var'_hd_ges!=.	//Decision: no missings allowed
}
}


* Transform data into long format

keep a_beteil* b_beteil* lfdn a_id* b_id*
reshape long a_beteil b_beteil, i(lfdn) j(welle)

	
* Individual linear regressions

/* For each individual we want to know whether reception increases, decreases or
remains the same during the campaign. This information shall be saved in `year'_koeff_`var'*/

foreach year in a b {
foreach var in beteil {

* Create outcome variable
gen `year'_koeff_`var' = .
gen `year'_rohkoeff_`var' = . 

* Loop over all respondents to calculate regression:
* fill target variable with values

sum `year'_id_`var'
forvalues num=1/`r(max)' {	
quietly reg `year'_`var' welle ///
	if `year'_id_`var'==`num'
replace `year'_rohkoeff_`var' = _b[welle] ///
	if `year'_id_`var'==`num'
gen p = 2*ttail(e(df_r), abs(_b[welle]/_se[welle])) ///
	if `year'_id_`var'==`num' 	//calculate p-value and save in variable
replace `year'_koeff_`var' = -1 ///
	if _b[welle]<0 & p<0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 0 ///
	if p>0.05 & `year'_id_`var'==`num' 
replace `year'_koeff_`var' = 1 ///
	if _b[welle]>0 & p<0.05 & `year'_id_`var'==`num' 
drop p

}
}
}


										
//Check number of cases
sum b_id_beteil
sum b_koeff_beteil 	//Correct!


* Save coefficients in new dataset since procedure is very time consuming
reshape wide a_beteil b_beteil ///
	a_koeff_beteil b_koeff_beteil ///
	a_rohkoeff_beteil b_rohkoeff_beteil, ///
	i(lfdn) j(welle)

keep lfdn ?_koeff_*1 ?_rohkoeff_*1 
							// The coefficient variable from one wave alone is 
							//sufficient, since the variable is constant
							//(the coefficient was calculated over all waves)

foreach ding in beteil {
rename ?_koeff_`ding'1 ?_koeff_`ding'		//Waveending gone, since constant
rename ?_rohkoeff_`ding'1 ?_rohkoeff_`ding'
}

lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient"
lab val ?_koeff_* koeff

save "${data}\data 2009 2013 IIR Chapter 4 turnout.dta", replace


*-----------------------------------------------------------------
* Table: Stability / Dynamic of Receptionfrequencies
*----------------------------------------------------------------

/* Measure of stability: cross average main diagonal percentages with intraindividual 
regression coefficients

In order to achieve this, first merge regression coefficients to dataset */

use "${data}\data2009_2013 participants 7 waves.dta",clear

//drop _merge
merge 1:1 lfdn using "${data}\data 2009 2013 IIR Chapter 4 turnout.dta"
drop _merge

lab define koeff ///
	-1 "negativer Koeffizient" ///
	0 "insignifikanter Koeffizient" ///
	1 "positiver Koeffizient", replace
lab val ?_koeff_* koeff


* Categorize intraindividual raw coefficients (without test of significance)


foreach year in a b {
	foreach var in beteil {
		gen `year'_rohkoeffkat_`var' = `year'_rohkoeff_`var'
		replace `year'_rohkoeffkat_`var' = -1 if `year'_rohkoeff_`var'<0
		replace `year'_rohkoeffkat_`var' = 1 if `year'_rohkoeff_`var'>0 & `year'_rohkoeff_`var'~=.
	}
}




* Final table		

**************************************************
***Table 36
**************************************************		


**Turnout

***Aggregation of wave means
estimates clear
foreach year in a b {
foreach var in beteil {

sum `year'_`var'1 if `year'_`var'_hd_ges~=.		//static mean, w1
gen `year'_mean`var'1 = r(mean)

sum `year'_`var'2 if `year'_`var'_hd_ges~=.		//static mean, w2
gen `year'_mean`var'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.		//static mean, w3
gen `year'_mean`var'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.		//static mean, w4
gen `year'_mean`var'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.		//static mean, w5
gen `year'_mean`var'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.		//static mean, w6
gen `year'_mean`var'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.		//static mean, w7
gen `year'_mean`var'7 = r(mean)

gen `year'_sum`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4+`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/7
tab1 `year'_sum`var'
gen `year'_sum14`var' = (`year'_mean`var'1+`year'_mean`var'2+`year'_mean`var'3+`year'_mean`var'4)/4
tab1 `year'_sum14`var'
gen `year'_sum57`var' = (`year'_mean`var'5+`year'_mean`var'6+`year'_mean`var'7)/3
tab1 `year'_sum57`var'

}
}

*Aggregate stability preparation


foreach year in a b{
	foreach var in beteil {
		foreach num of numlist 1/7 {
	gen `year'_`var'`num'n = round(`year'_`var'`num'*4)
	tab `year'_`var'`num'n
}
}
}
*

*Differences

foreach year in a b{
	foreach var in beteil {
gen `year'_diff_`var'12=`year'_`var'2n-`year'_`var'1n
tab `year'_diff_`var'12
	
gen `year'_diff_`var'23=`year'_`var'3n-`year'_`var'2n
tab `year'_diff_`var'23

gen `year'_diff_`var'34=`year'_`var'4n-`year'_`var'3n
tab `year'_diff_`var'34

gen `year'_diff_`var'45=`year'_`var'5n-`year'_`var'4n
tab `year'_diff_`var'45

gen `year'_diff_`var'56=`year'_`var'6n-`year'_`var'5n
tab `year'_diff_`var'56

gen `year'_diff_`var'67=`year'_`var'7n-`year'_`var'6n
tab `year'_diff_`var'67

gen `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/4) if `year'_`var'1n==0 | `year'_`var'1n==4
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/3) if `year'_`var'1n==1 | `year'_`var'1n==3
replace `year'_stab_`var'12 =1-abs(`year'_diff_`var'12/2) if `year'_`var'1n==2
tab `year'_stab_`var'12

gen `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/4) if `year'_`var'2n==0 | `year'_`var'2n==4
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/3) if `year'_`var'2n==1 | `year'_`var'2n==3
replace `year'_stab_`var'23 =1-abs(`year'_diff_`var'23/2) if `year'_`var'2n==2
tab `year'_stab_`var'23

gen `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/4) if `year'_`var'3n==0 | `year'_`var'3n==4
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/3) if `year'_`var'3n==1 | `year'_`var'3n==3
replace `year'_stab_`var'34 =1-abs(`year'_diff_`var'34/2) if `year'_`var'3n==2
tab `year'_stab_`var'34

gen `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/4) if `year'_`var'4n==0 | `year'_`var'4n==4
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/3) if `year'_`var'4n==1 | `year'_`var'4n==3
replace `year'_stab_`var'45 =1-abs(`year'_diff_`var'45/2) if `year'_`var'4n==2
tab `year'_stab_`var'45

gen `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/4) if `year'_`var'5n==0 | `year'_`var'5n==4
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/3) if `year'_`var'5n==1 | `year'_`var'5n==3
replace `year'_stab_`var'56 =1-abs(`year'_diff_`var'56/2) if `year'_`var'5n==2
tab `year'_stab_`var'56

gen `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/4) if `year'_`var'6n==0 | `year'_`var'6n==4
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/3) if `year'_`var'6n==1 | `year'_`var'6n==3
replace `year'_stab_`var'67 =1-abs(`year'_diff_`var'67/2) if `year'_`var'6n==2
tab `year'_stab_`var'67

}
}


foreach year in a b{
	foreach var in beteil {
		foreach num in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'`num'
			gen `year'_stab_`var'`num'_mean = r(mean)
}
}
}
*

*Aggregate stability
foreach year in a b{
	foreach var in beteil {
		gen `year'_stab_`var'_aggr=(`year'_stab_`var'12_mean+`year'_stab_`var'23_mean+`year'_stab_`var'34_mean+`year'_stab_`var'45_mean+`year'_stab_`var'56_mean+`year'_stab_`var'67_mean)/6
		tab `year'_stab_`var'_aggr
}
}
*


** Separate analyses for political interest and existance of party identification

**Separate for political interest

***Aggregation of wave means
estimates clear
foreach year in a b {
foreach num in 0 1 2 {
foreach var in beteil {

sum `year'_`var'1 if `year'_`var'_hd_ges~=. & `year'_polinttri1==`num'		//static mean, w1
gen `year'_mean`var'_pol`num'1 = r(mean)

sum `year'_`var'2 if `year'_`var'_hd_ges~=. & `year'_polinttri1==`num'		//static mean, w2
gen `year'_mean`var'_pol`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w3
gen `year'_mean`var'_pol`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w4
gen `year'_mean`var'_pol`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w5
gen `year'_mean`var'_pol`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w6
gen `year'_mean`var'_pol`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_polinttri1==`num'	//static mean, w7
gen `year'_mean`var'_pol`num'7 = r(mean)

gen `year'_sum`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'2+`year'_mean`var'_pol`num'3+`year'_mean`var'_pol`num'4+`year'_mean`var'_pol`num'5+`year'_mean`var'_pol`num'6+`year'_mean`var'_pol`num'7)/7
tab1 `year'_sum`var'_pol`num'
gen `year'_sum14`var'_pol`num' = (`year'_mean`var'_pol`num'1+`year'_mean`var'_pol`num'2+`year'_mean`var'_pol`num'3+`year'_mean`var'_pol`num'4)/4
tab1 `year'_sum14`var'_pol`num'
gen `year'_sum57`var'_pol`num' = (`year'_mean`var'_pol`num'5+`year'_mean`var'_pol`num'6+`year'_mean`var'_pol`num'7)/3
tab1 `year'_sum57`var'_pol`num'

}
}
}
*Differences

foreach year in a b{
foreach num in 0 1 2{
	foreach var in beteil {
gen `year'_diff_`var'_pol`num'12=`year'_`var'2n-`year'_`var'1n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'12
	
gen `year'_diff_`var'_pol`num'23=`year'_`var'3n-`year'_`var'2n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'23

gen `year'_diff_`var'_pol`num'34=`year'_`var'4n-`year'_`var'3n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'34

gen `year'_diff_`var'_pol`num'45=`year'_`var'5n-`year'_`var'4n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'45

gen `year'_diff_`var'_pol`num'56=`year'_`var'6n-`year'_`var'5n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'56

gen `year'_diff_`var'_pol`num'67=`year'_`var'7n-`year'_`var'6n if `year'_polinttri1==`num'
tab `year'_diff_`var'_pol`num'67

gen `year'_stab_`var'_pol`num'12 =1-abs(`year'_diff_`var'_pol`num'12/4) if `year'_`var'1n==0 | `year'_`var'1n==4
replace `year'_stab_`var'_pol`num'12 =1-abs(`year'_diff_`var'_pol`num'12/3) if `year'_`var'1n==1 | `year'_`var'1n==3
replace `year'_stab_`var'_pol`num'12 =1-abs(`year'_diff_`var'_pol`num'12/2) if `year'_`var'1n==2
tab `year'_stab_`var'_pol`num'12

gen `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/4) if `year'_`var'2n==0 | `year'_`var'2n==4
replace `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/3) if `year'_`var'2n==1 | `year'_`var'2n==3
replace `year'_stab_`var'_pol`num'23 =1-abs(`year'_diff_`var'_pol`num'23/2) if `year'_`var'2n==2
tab `year'_stab_`var'_pol`num'23

gen `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/4) if `year'_`var'3n==0 | `year'_`var'3n==4
replace `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/3) if `year'_`var'3n==1 | `year'_`var'3n==3
replace `year'_stab_`var'_pol`num'34 =1-abs(`year'_diff_`var'_pol`num'34/2) if `year'_`var'3n==2
tab `year'_stab_`var'_pol`num'34

gen `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/4) if `year'_`var'4n==0 | `year'_`var'4n==4
replace `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/3) if `year'_`var'4n==1 | `year'_`var'4n==3
replace `year'_stab_`var'_pol`num'45 =1-abs(`year'_diff_`var'_pol`num'45/2) if `year'_`var'4n==2
tab `year'_stab_`var'_pol`num'45

gen `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/4) if `year'_`var'5n==0 | `year'_`var'5n==4
replace `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/3) if `year'_`var'5n==1 | `year'_`var'5n==3
replace `year'_stab_`var'_pol`num'56 =1-abs(`year'_diff_`var'_pol`num'56/2) if `year'_`var'5n==2
tab `year'_stab_`var'_pol`num'56

gen `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/4) if `year'_`var'6n==0 | `year'_`var'6n==4
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/3) if `year'_`var'6n==1 | `year'_`var'6n==3
replace `year'_stab_`var'_pol`num'67 =1-abs(`year'_diff_`var'_pol`num'67/2) if `year'_`var'6n==2
tab `year'_stab_`var'_pol`num'67

}
}
}

foreach year in a b{
foreach num in 0 1 2{
	foreach var in beteil {
		foreach num2 in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'_pol`num'`num2'
			gen `year'_stab_`var'_pol`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a b{
foreach num in 0 1 2{
	foreach var in beteil {
		gen `year'_stab_`var'_pol`num'_aggr=(`year'_stab_`var'_pol`num'12_mean+`year'_stab_`var'_pol`num'23_mean+`year'_stab_`var'_pol`num'34_mean+`year'_stab_`var'_pol`num'45_mean+`year'_stab_`var'_pol`num'56_mean+`year'_stab_`var'_pol`num'67_mean)/6
		tab `year'_stab_`var'_pol`num'_aggr
}
}
}
*



**Separate analysis for party identification

***Aggregation of wave means
estimates clear
foreach year in a b {
foreach num in 0 1 {
foreach var in beteil {

sum `year'_`var'1 if `year'_`var'_hd_ges~=. & `year'_pidyes1==`num'		//static mean, w1
gen `year'_mean`var'_pid`num'1 = r(mean)

sum `year'_`var'2 if `year'_`var'_hd_ges~=. & `year'_pidyes1==`num'		//static mean, w2
gen `year'_mean`var'_pid`num'2 = r(mean)

sum `year'_`var'3 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w3
gen `year'_mean`var'_pid`num'3 = r(mean)

sum `year'_`var'4 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w4
gen `year'_mean`var'_pid`num'4 = r(mean)

sum `year'_`var'5 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w5
gen `year'_mean`var'_pid`num'5 = r(mean)

sum `year'_`var'6 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w6
gen `year'_mean`var'_pid`num'6 = r(mean)

sum `year'_`var'7 if `year'_`var'_hd_ges~=.	 & `year'_pidyes1==`num'	//static mean, w7
gen `year'_mean`var'_pid`num'7 = r(mean)

gen `year'_sum`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4+`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/7
tab1 `year'_sum`var'_pid`num'
gen `year'_sum14`var'_pid`num' = (`year'_mean`var'_pid`num'1+`year'_mean`var'_pid`num'2+`year'_mean`var'_pid`num'3+`year'_mean`var'_pid`num'4)/4
tab1 `year'_sum14`var'_pid`num'
gen `year'_sum57`var'_pid`num' = (`year'_mean`var'_pid`num'5+`year'_mean`var'_pid`num'6+`year'_mean`var'_pid`num'7)/3
tab1 `year'_sum57`var'_pid`num'

}
}
}
*Differences

foreach year in a b{
foreach num in 0 1{
	foreach var in beteil {
gen `year'_diff_`var'_pid`num'12=`year'_`var'2n-`year'_`var'1n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'12
	
gen `year'_diff_`var'_pid`num'23=`year'_`var'3n-`year'_`var'2n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'23

gen `year'_diff_`var'_pid`num'34=`year'_`var'4n-`year'_`var'3n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'34

gen `year'_diff_`var'_pid`num'45=`year'_`var'5n-`year'_`var'4n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'45

gen `year'_diff_`var'_pid`num'56=`year'_`var'6n-`year'_`var'5n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'56

gen `year'_diff_`var'_pid`num'67=`year'_`var'7n-`year'_`var'6n if `year'_pidyes1==`num'
tab `year'_diff_`var'_pid`num'67

gen `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/4) if `year'_`var'1n==0 | `year'_`var'1n==4
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/3) if `year'_`var'1n==1 | `year'_`var'1n==3
replace `year'_stab_`var'_pid`num'12 =1-abs(`year'_diff_`var'_pid`num'12/2) if `year'_`var'1n==2
tab `year'_stab_`var'_pid`num'12

gen `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/4) if `year'_`var'2n==0 | `year'_`var'2n==4
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/3) if `year'_`var'2n==1 | `year'_`var'2n==3
replace `year'_stab_`var'_pid`num'23 =1-abs(`year'_diff_`var'_pid`num'23/2) if `year'_`var'2n==2
tab `year'_stab_`var'_pid`num'23

gen `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/4) if `year'_`var'3n==0 | `year'_`var'3n==4
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/3) if `year'_`var'3n==1 | `year'_`var'3n==3
replace `year'_stab_`var'_pid`num'34 =1-abs(`year'_diff_`var'_pid`num'34/2) if `year'_`var'3n==2
tab `year'_stab_`var'_pid`num'34

gen `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/4) if `year'_`var'4n==0 | `year'_`var'4n==4
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/3) if `year'_`var'4n==1 | `year'_`var'4n==3
replace `year'_stab_`var'_pid`num'45 =1-abs(`year'_diff_`var'_pid`num'45/2) if `year'_`var'4n==2
tab `year'_stab_`var'_pid`num'45

gen `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/4) if `year'_`var'5n==0 | `year'_`var'5n==4
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/3) if `year'_`var'5n==1 | `year'_`var'5n==3
replace `year'_stab_`var'_pid`num'56 =1-abs(`year'_diff_`var'_pid`num'56/2) if `year'_`var'5n==2
tab `year'_stab_`var'_pid`num'56

gen `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/4) if `year'_`var'6n==0 | `year'_`var'6n==4
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/3) if `year'_`var'6n==1 | `year'_`var'6n==3
replace `year'_stab_`var'_pid`num'67 =1-abs(`year'_diff_`var'_pid`num'67/2) if `year'_`var'6n==2
tab `year'_stab_`var'_pid`num'67

}
}
}

foreach year in a b{
foreach num in 0 1{
	foreach var in beteil {
		foreach num2 in 12 23 34 45 56 67 {
			ci means `year'_stab_`var'_pid`num'`num2'
			gen `year'_stab_`var'_pid`num'`num2'_mean = r(mean)
}
}
}
}
*

*Aggregate stability
foreach year in a b{
foreach num in 0 1{
	foreach var in beteil {
		gen `year'_stab_`var'_pid`num'_aggr=(`year'_stab_`var'_pid`num'12_mean+`year'_stab_`var'_pid`num'23_mean+`year'_stab_`var'_pid`num'34_mean+`year'_stab_`var'_pid`num'45_mean+`year'_stab_`var'_pid`num'56_mean+`year'_stab_`var'_pid`num'67_mean)/6
		tab `year'_stab_`var'_pid`num'_aggr
}
}
}
*





********************************************************************************


***Percentage intra-individual change

foreach year in a b {
	foreach var in beteil {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' [aw=`year'_weight], cell
}
}

**Separate for political interest

foreach year in a b {
foreach num in 0 1 2 {
	foreach var in beteil {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' if `year'_polinttri1==`num' [aw=`year'_weight], cell
}
}
}


**Separate for existance of party identification

foreach year in a b {
foreach num in 0 1 {
	foreach var in beteil {
		tab `year'_`var'_hd_ges `year'_rohkoeffkat_`var' if `year'_pidyes1==`num' [aw=`year'_weight], cell
}
}
}



*Drop unneccessary auxilliary variables

foreach year in a b{
	foreach var in beteil {
	drop `year'_diff_`var'??
	drop `year'_diff_`var'_pol???
	drop `year'_diff_`var'_pid???
	drop `year'_stab_`var'??
	drop `year'_stab_`var'_pol???
	drop `year'_stab_`var'_pid???
	drop `year'_stab_`var'??_mean
	drop `year'_stab_`var'_pol???_mean
	drop `year'_stab_`var'_pid???_mean
	drop `year'_`var'?n
}
}


save "${data}\data2009_2013 participants 7 waves.dta", replace

	